﻿using log4net;
using ServerControl.Dal;
using ServerControl.Model;
using ServerControl.WebManage.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ServerControl.Utils;
using ServerControl.Model.Enum;
using System.Text;
using MiniExcelLibs;
using System.IO;
using System.Web.Http.Results;
using static System.Net.Mime.MediaTypeNames;
using WebGrease.Css.Extensions;
using MiniExcelLibs.OpenXml;
using System.Security.Principal;
using Newtonsoft.Json;

namespace ServerControl.WebManage.Controllers.Admin
{
    [RoutePrefix("admin/video")]
    [Auth(AllowRole = Model.Enum.Role.AdminOrUser)]
    public class VideoController : BaseController
    {
        private ILog logger = LogManager.GetLogger("VideoController");

        [Route("index")]
        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        [Route("list")]
        public JsonResult ListPage(int pageIndex = 1, int pageSize = 20, string remark = "")
        {
            try
            {
                int records = 0;
                var datas = VideoDal.ListPage(ref records, pageIndex, limit: pageSize, CurrentUser.UserName, remark);
                return new JsonNetResult { Data = new SuccessResult<List<Video>> { Data = datas, Count = records, Msg = "" } };
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
        }

        [HttpPost]
        [Route("delete")]
        public JsonResult Delete(List<int> ids)
        {
            try
            {
                var comments = CommentDal.SelectByVideoIds(CurrentUser.UserName, ids).Where(c => c.ContentType == ContentTypeEnum.Image).ToList(); ;

                if (VideoDal.Delete(CurrentUser.UserName, ids))
                {
                    comments.ForEach(c =>
                    {
                        try
                        {
                            var filePath = Server.MapPath(c.Content);

                            if (System.IO.File.Exists(filePath))
                            {
                                System.IO.File.Delete(filePath);
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex);
                        }
                    });

                    return new JsonNetResult { Data = new SuccessResult { Msg = "删除成功" } };
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "删除失败" } };
        }

        [HttpPost]
        [Route("edit")]
        public JsonResult Edit(Video video)
        {
            try
            {
                video.UserName = CurrentUser.UserName;

                if (VideoDal.Update(video))
                {
                    return new JsonNetResult { Data = new SuccessResult { Msg = "操作成功" } };
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "操作失败" } };
        }

        [HttpPost]
        [Route("add")]
        public JsonResult Add(Video video)
        {
            try
            {
                video.UserName = CurrentUser.UserName;
                video.AddTime = DateTime.Now;

                if (VideoDal.Insert(video))
                {
                    return new JsonNetResult { Data = new SuccessResult { Msg = "操作成功" } };
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "操作失败" } };
        }
    }
}